//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using OpenLS.Chart.Internals;
using OpenLS.Core.Serialization;

namespace OpenLS.Chart
{
    //internal delegate void ValueChangedEventHandler(object sender, EventArgs args);

    ///<summary>
    /// Represents a 2D pie series is a pie chart
    ///</summary>
    public class PieSeries : Series, INumericValuesProvider, IDataPointOwner, IDataLabelsOwner, INotifyValueChanged
    {
        private readonly DataPoints _dpt = new DataPoints();
        private DataSource _cat;
        private DataLabels _dlbls;
        private int _explosion;
        private NumericDataSource _values;


        public int Explosion
        {
            get { return _explosion; }
            set
            {
                _explosion = value;
                notifyPropertyChanged("Explosion");
            }
        }

        public DataSource Categories
        {
            get { return _cat; }
            set
            {
                _cat = value;
                notifyPropertyChanged("Categories");
            }
        }

        public NumericDataSource Values
        {
            get { return _values; }
            set
            {
                if (_values != null)
                    _values.ValueChanged -= ValuesValueChanged;
                _values = value;
                notifyPropertyChanged("Values");
                if (_values != null)
                    _values.ValueChanged += ValuesValueChanged;
            }
        }

        #region IDataLabelsOwner Members

        public DataLabels DataLabels
        {
            get { return _dlbls; }
            set
            {
                _dlbls = value;
                notifyPropertyChanged("DataLabels");
            }
        }

        #endregion

        #region IDataPointOwner Members

        public DataPoints DataPoints
        {
            get { return _dpt; }
        }

        #endregion

        #region INumericValuesProvider Members

        IEnumerable<NumericValue> INumericValuesProvider.GetValues()
        {
            return Values.NumericValues;
        }

        public event EventHandler ValueChanged;

        #endregion

        private void ValuesValueChanged(object sender, EventArgs e)
        {
            if (ValueChanged != null)
                ValueChanged(this, new EventArgs());
        }


        //\\    public event EventHandler ValueChanged;

        internal override void ReadXmlCore(ReadContext context)
        {
            using (ReadContext c = context.Read("ser"))
            {
                ReadXmlBase(c);
                Explosion = c.ReadOptionalElementIntegerAttribute("explosion", "val", 0);
                DataPoints.Clear();
                while (c.ReaderLocalName == "dPt")
                {
                    DataPoints.Add(c.ReadElement<DataPoint>());
                }
                DataLabels = c.ReadOptionalElement<DataLabels>(DataLabels.xmlName);
                Categories = DataSource.ReadOptionalDataSource("cat", c);
                Values = NumericDataSource.ReadOptionalNumericDataSource("val", c);
                c.WarnIf("crtExt");
            }
        }

        internal override void WriteXmlCore(WriteContext context)
        {
            using (WriteContext c = context.Write("ser"))
            {
                WriteXmlBase(c);
                c.WriteOptionalElementIntegerAttribute("explosion", "val", Explosion, 0);
                foreach (DataPoint dPt in DataPoints)
                    c.WriteElement(dPt);
                c.WriteOptionalElement(DataLabels);
                WriteOptionalNumericDataSource("cat", Categories, c);
                WriteOptionalNumericDataSource("val", Values, c);
                c.WarnIf("crtExt");
            }
        }
    }
}